﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using XCI.Extensions;
using XCI.Helper;
using XCI.Windows.Forms;
using XCI.Windows.Helper;
using XCI.Sys.Settings;

namespace XCI.Sys.Forms
{
    public partial class FrmModernLogin : FrmBaseLogin
    {
        public FrmModernLogin()
        {
            InitializeComponent();
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
        }

        protected override Control EditAccount => editLoginName;

        protected override Control EditPassword => editPassword;

        protected override void LoadLoginSetting()
        {
            var setting = LoginModernSetting.Instance;

            //LoadBackgroundImage();
            var imagePath = IOHelper.GetOutFilePath(setting.BackgroundImage);
            if (File.Exists(imagePath))
            {
                this.BackgroundImage = ImageHelper.LoadImage(imagePath);
            }
            else if (setting.BackgroundColor.IsNotEmpty())
            {
                this.BackgroundImage = null;
            }

            //BackColor
            string bgColor = setting.BackgroundColor;
            if (bgColor.IsNotEmpty())
            {
                this.BackColor = (Color) ObjectHelper.GetObjectFromString(bgColor, typeof(Color));
            }

            //LoadUserImage();
            imagePath = IOHelper.GetOutFilePath(setting.UserImage);
            if (File.Exists(imagePath))
            {
                picUserImage.Image = ImageHelper.LoadImage(imagePath);
            }
            else
            {
                picUserImage.Image = XCI.Sys.Properties.Resources.usertile;
            }

            //LoadOpacity();
            int opacity = setting.Opacity.ToInt();
            if (opacity > 0)
            {
                this.Opacity = Convert.ToDouble(opacity) / 100;
            }

            //LoadTitle();
            string title = SysRuntime.Setting.Title;
            this.Text = string.Concat(title, "  ", "系统登录");
            this.labAppTitle.Text = title;
            WinHelper.CenterControl(this, labAppTitle, this, true, false);

            //editLoginName.Text = StandardLoginSetting.Instance.RememberLoginName;
            //if (StringHelper.IsBase64(StandardLoginSetting.Instance.RememberPassword))
            //{
            //    editPassword.Text = EncryptHelper.SymmetricDecrypt(StandardLoginSetting.Instance.RememberPassword, EncryptHelper.SecretKey);
            //}
            //if (StandardLoginSetting.Instance.AutoLogin)
            //{
            //    ExecuteAutoLogin();
            //}
        }

        private void FrmModernLogin_Load(object sender, EventArgs e)
        {
            LoadLoginSetting();
            //NativeMethods.SetForegroundWindow(this.Handle);
        }

        private void editLoginName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && editLoginName.Text.Trim().Length > 0)
            {
                editPassword.Select();
                e.Handled = true;
            }
        }

        private void editPassowrd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.LoginCore();
                e.Handled = true;
            }
            else if (e.KeyChar == 27)
            {
                editLoginName.Focus();
                editLoginName.SelectionStart = editLoginName.Text.Length;
                e.Handled = true;
            }
        }
    }
}